package com.example.administrator.ygddriver.liteorm;


import android.content.Context;
import android.util.Log;

import com.example.administrator.ygddriver.base.BaseHint;
import com.example.administrator.ygddriver.base.UserBean;

/**
 * Created by Administrator on 2017/2/21.
 */
public class LiteOrmTools {

    //    public static <T> List<T> getQueryByWhere(Class<T> cla, String field, String[] activity_weight_message) {
//        return SingleLiteOrm.getSingleLiteOrm(MyApp.context).SingleLite().<T>query(new QueryBuilder(cla).where(field + "=?", activity_weight_message));
//    }
//
//    public static <T> List<T> getQueryByWhereLength(Class<T> cla, int start, int length) {
//        return SingleLiteOrm.getSingleLiteOrm(MyApp.context).SingleLite().<T>query(new QueryBuilder(cla).appendOrderDescBy("id").limit(start, length));
//    }


    public static void getUserMessage(Context context, OnTrue onTrue) {
        if (SingleLiteOrm.getSingleLiteOrm(context).SingleLite().query(UserBean.class).size() > 0) {
            UserBean userBean = SingleLiteOrm.getSingleLiteOrm(context).SingleLite().query(UserBean.class).get(0);
            onTrue.hasData(userBean);
        } else {
            BaseHint baseHint = new BaseHint.addHint().setParameter(context, "提示", "登录失效请重新登陆");
            baseHint.show();
            return;
        }

    }

    public static <T> void getInformation(Class<T> cla, Context context, On<T> on, OFF off) {
        Log.d("131321", "SingleLiteOrm.getSingleLiteOrm(context).SingleLite().query(cla).size():" + SingleLiteOrm.getSingleLiteOrm(context).SingleLite().query(cla).size());
        if (SingleLiteOrm.getSingleLiteOrm(context).SingleLite().query(cla).size() > 0) {
            on.hasData((T) SingleLiteOrm.getSingleLiteOrm(context).SingleLite().query(cla).get(0));
        } else {
            off.noData();
        }

    }

    public interface On<T> {
        void hasData(T data);
    }

    public interface OFF<T> {
        void noData();
    }

    public interface OnTrue {
        void hasData(UserBean data);
    }

    public interface OnError {
        void noData();
    }

}
